package com.hubery.designpattern.design.template.gant;

import com.hubery.designpattern.design.decorator.dto.UserInfoDTO;
import com.hubery.designpattern.dto.PrizeProvideReqDTO;
import com.hubery.designpattern.enums.UserTypeEnum;
import com.hubery.designpattern.design.template.commodity.CommodityServiceSupport;
import com.hubery.designpattern.event.ActivityNotifyUserEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author lixueyang
 * @since 2023/2/27 20:51
 */
@Slf4j
@Component
public abstract class AbstractGantPrizeTemplate extends CommodityServiceSupport {

    @Resource
    private ApplicationContext applicationContext;

    public void process(UserInfoDTO dto, PrizeProvideReqDTO reqDTO) {

        //验证用户是否拥有资格
        if (!checkQualification(dto)) {
            return;
        }

        getCommodityServiceService(reqDTO.getPrizeTypeEnum()).process(reqDTO);

        //通知用户
        notifyUser(dto);
    }

    private void notifyUser(UserInfoDTO dto) {
        log.info("通知用户 【{}】, 奖品已发放到位", dto.getUsername());

        applicationContext.publishEvent(ActivityNotifyUserEvent
                .builder()
                .userInfoDTO(dto)
                .msg("奖品已发放到位")
                .build());
    }

    abstract protected boolean checkQualification(UserInfoDTO dto);

    abstract protected UserTypeEnum getUserTypeEnum();
}
